package com.example.reference_book.controller;

import com.example.reference_book.entity.School;
import com.example.reference_book.service.SchoolService;
import com.example.reference_book.util.SchoolSearchUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学校表(School)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:19:04
 */
@RestController
@RequestMapping("school")
public class SchoolController {
    /**
     * 服务对象
     */
    @Resource
    private SchoolService schoolService;

    private SchoolSearchUtil searchUtil = SchoolSearchUtil.getSearchUtil();

    /**
     * 查询
     * @return 查询结果
     */

    @GetMapping("/searchByKey/{key}")
    public ResponseEntity<List<School>> searchByKey(@PathVariable("key") String key){
        return ResponseEntity.ok(this.searchUtil.searchByKey(key));
    }

    @PostMapping("/selectAll")
    public ResponseEntity<List<School>> selectAll() {
        List<School> list = this.schoolService.queryAll();
        searchUtil.init(list);
        return ResponseEntity.ok(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<School> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.schoolService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param school 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<School> add(School school) {
        return ResponseEntity.ok(this.schoolService.insert(school));
    }

    /**
     * 编辑数据
     *
     * @param school 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseEntity<School> edit(School school) {
        return ResponseEntity.ok(this.schoolService.update(school));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.schoolService.deleteById(id));
    }

}

